// wkt: 坐标点集的WKT字符串，格式： GEOMETRYCOLLECTION (POINT (114 23), POINT (115 22), POINT (113 23))
function showGeomList(wkt, mapName) {
    console.info(wkt)

    function a() {
        var firstLeftIndex = wkt.indexOf('(');
        var lastRightIndex = wkt.lastIndexOf(')');
        // 去掉首尾括号
        var str = wkt.substring(firstLeftIndex + 1, lastRightIndex);
        var pts = str.split(",");

        var lnglats = new Array();
        for (i = 0; i < pts.length; i++) {
            var pt = pts[i];
            var left = pt.indexOf('(');
            var right = pt.indexOf(')');

            var xy = pt.substr(left + 1, right).split(' ');
            if (xy[1]) {
                var lng = parseFloat(xy[1])
                var lat = parseFloat(xy[0])
                // 中国地区，总是：经度 > 纬度
                if (lng < lat) {
                    var tmp = lng;
                    lng = lat;
                    lat = tmp;
                }
                lnglats[i] = [lng, lat];
            }
        }

        return lnglats;
    }

    var lnglats = a();
    console.info(lnglats)

    var map = new AMap.Map(mapName, {resizeEnable: true});
    var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
    for (var i = 0;i < lnglats.length; i++) {
        var marker = new AMap.Marker({
            position: lnglats[i],
            map: map,
            label: {
                offset: new AMap.Pixel(20, 20),//修改label相对于maker的位置
                content: "点击坐标" + i + "打开高德地图"
            }
        });
        marker.on('click',function(e){
            e.target.markOnAMAP({
                position:e.target.position
            })
        });
    }
    function markerClick(e) {
        infoWindow.setContent(e.target.content);
        infoWindow.open(map, e.target.getPosition());
    }
    map.setFitView();
    var polyline = new AMap.Polyline({
        path: lnglats,
        strokeColor: "#00A",  //线颜色
        strokeOpacity: 3,     //线透明度
        strokeWeight: 3,      //线宽
        strokeStyle: "dashed"  //线样式
    });
    polyline.setMap(map);
}

/**
 * show current position.
 * @param mapName element name to hold map
 * @param holderName wkt holder name
 * @param statusName to show status of position.
 * @param resultName to show result of position.
 */
function showCurrentPos(mapName, holderName, statusName, resultName) {
    var map = new AMap.Map(mapName, {
        resizeEnable: true
    });

    AMap.plugin('AMap.Geolocation', function () {
        var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 10000,          //超过10秒后停止定位，默认：5s
            buttonPosition: 'RB',    //定位按钮的停靠位置
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true,   //定位成功后是否自动调整地图视野到定位点
        });

        //使用远程定位，见 remogeo.js
        var remoGeo = new RemoGeoLocation();

        //替换方法
        navigator.geolocation.getCurrentPosition = function() {
            return remoGeo.getCurrentPosition.apply(remoGeo, arguments);
        };

        //替换方法
        navigator.geolocation.watchPosition = function() {
            return remoGeo.watchPosition.apply(remoGeo, arguments);
        };

        map.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
        AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
    });

    //解析定位结果
    function onComplete(data) {
        console.info(data.position);
        document.getElementById(holderName).value = 'GEOMETRYCOLLECTION(POINT(' +
            data.position.lat + ' ' + data.position.lng + '))';

        document.getElementById(statusName).innerHTML = '定位成功';
        var str = [];
        str.push('定位结果：' + data.position);
        str.push('定位类别：' + data.location_type);
        if (data.accuracy) {
            str.push('精度：' + data.accuracy + ' 米');
        }//如为IP精确定位结果则没有精度信息
        str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
        document.getElementById(resultName).innerHTML = str.join('<br>');

    }

    //解析定位错误信息
    function onError(data) {
        document.getElementById(statusName).innerHTML = '定位失败(用DMA位置替代)';
        document.getElementById(resultName).innerHTML = '失败原因排查信息:' + data.message;
    }
}